ScriptX provides two different kinds of scope for its variables: global and local. Variables that are declared global
have a global scope. This means they continue to exist for the duration of the program's execution, and they are visible in modules that use the current module. Variables that are declared local
, however, can be declared and used only within the context of a limited variable scope. Local declarations cease to exist once that variable scope has ended.
ScriptX does not directly define a static scope, as in C and C++, but equivalent functionality is available through closures. A closure variable is a variable that persists in memory like a global variable, but is visible only within a local scope. Closures and closure variables are defined on page 103.
The ScriptX language has several constructs that introduce a new variable scope different from that of the scope surrounding it, including function and method definitions and some loops. Compound expressions are the most commonly used construct that defines a new variable scope. See "Compound Expressions" on page 52 for more information about local variables and examples of how variables act within different variable scopes.
By default, ScriptX variables are global, including the variables that are used to name functions, classes, and some objects. However, you should explicitly declare each variable you use to be either local
or global
using the expressions described in the section "Declaring Variables" on page 37.
modules
. Modules in ScriptX are a packaging system that allows control over multiple global variable namespaces. Global variable names are only visible within the module where they are declared, and in modules that use that module. Modules allow for integration of large titles from components made by many different programmers without naming conflicts.Every ScriptX expression is compiled and executed within the context of a given module. Global variables defined and used within a single module are only visible to other expressions that are visible within that module. Different modules can have different global variables with the same name. Using modules, you can import, export, and rename variables across modules to combine parts of ScriptX programs without worrying about whether different parts of a program use the same names.
You do not need to define modules, or even understand modules, in order to use ScriptX. Most ScriptX environments provide a default module in which you can define variables, enter expressions, execute scripts, and experiment with ScriptX. ScriptX modules are useful when you are creating larger programs or parts of programs that you expect to distribute and be used by other ScriptX programs. Chapter 9, "Modules," describes how to define and use multiple modules to manage variables in your ScriptX program.
local
or global
construct.local varname, varname, varname . . .The varname is the name of the variable to be declared. Multiple variable names can be specified in the same expression, separated by commas.
global varname, varname, varname . . .
Local variables can only be declared and referred to by name inside a construct that introduces a new variable scope (such as a compound expression, described on page 52). You cannot declare them at the outermost level.
global density, mass, velocity
You do not have to declare variables before they are used. Be forewarned, however, that the first time you reference a variable that has not yet been declared (either by assigning an object to it or by simply referring to it by name), that such a variable is declared global
(regardless of the scope in which it is defined) and a warning is printed to your environment's debugging window or stream:
mishegoss := @normal
-- ** Warning: Undeclared global mishegoss
@normal
For the sake of brevity, many examples in this book do not explicitly declare global variables. Readers should assume that any global variables used in examples have already been declared.
If you try to reference a variable that has been declared, but has not yet had anything assigned to it, ScriptX reports an exception.
global pickup
print pickup
-- ** While calling from the listener at:
0: PUSH-EXTERNAL-VARIABLE Scratch:pickup
-- ** Scratch:pickup does not have a variable value
(UninitializedVariable)
Variables can be assigned initial values using the assignment operator ( :=
), which is described in the section "Assignment" on page 38. A colon can also be used to assign an initial value to a variable.
global pickup := 52
global defenseBudget:3.18e11, moonsOfMars:2
local
or global
reserved word with the reserved word constant
. Constants have values that cannot be changed by assignment, and as such must have values assigned to them at time of declaration:global constant halfTon := 1000
global constant lightSpeed := 2.998e8 -- speed of light, meters/sec
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.